from django.shortcuts import render
from rest_framework.pagination import PageNumberPagination
from rest_framework.views import  APIView
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from unicodedata import category

from course import models,courseser
from django.db.models import Q
import json
# Create your views here.
class HomeCate(APIView):
    def get(self,request):
        allcate=models.Category.objects.filter(parent=None,flag=True).order_by("order")
        ser = courseser.CategorySerializer(allcate, many=True)
        data=ser.data
        for item in data:
            item["children"]= item["children"][:3]
        return Response({"code":200,"msg":"ok","data":data})

class HomeChild(APIView):
    def get(self,request):
        cate=request.query_params.get("cate","")
        if not cate:
            return Response("参数不对")
        catelist=models.Category.objects.get(id=cate,flag=True)
        ser = courseser.CategorySerializer(catelist)
        data=ser.data
        child=[]
        for item in data["children"]:
            child.append(item["id"])
        course=models.Course.objects.filter(cate__in=child,flag=True).order_by("order")[:4]
        courser=courseser.CourseListSer(course,many=True)
        data["course"]=courser.data
        return Response({"code":200,"msg":"ok","data":data})

class ParentCate(APIView):
    def get(self,request):
        #查询所有的一级分类 并且是有效的数据  还得排序
        cate=models.Category.objects.filter(parent=None,flag=True).order_by("order")
        #序列化
        ser=courseser.CategorySer(cate,many=True)
        return Response({"code":200,"msg":"ok","data":ser.data})


class ChildCate(APIView):
    def get(self,request):
        #获取一级分类的id
        parent=request.query_params.get("parent",None)
        if not parent:
            return Response("参数不对")
        if parent=="0":
            cate=models.Category.objects.exclude(parent=None,flag=True).order_by("order")
        else:
            cate=models.Category.objects.filter(parent=parent,flag=True).order_by("order")

        ser=courseser.CategorySer(cate,many=True)
        return Response({"code":200,"msg":"ok","data":ser.data})


class PageCourse(PageNumberPagination):
    page_size=4
    page_size_query_param = 'size'

class CourseList(ListAPIView):
    queryset=models.Course.objects.filter(flag=True).order_by("order")
    serializer_class=courseser.CourseListSer
    pagination_class=PageCourse
    def get_queryset(self):
        diretion=self.request.query_params.get("direction",None)
        category=self.request.query_params.get("category",None)
        level=self.request.query_params.get("level",None)
        ordering=self.request.query_params.get("ordering",None)
        query=Q(flag=True)
        if category and category!="0":
            query&=Q(cate=category)
        if diretion!="0" and category=="0":
            allchild=models.Category.objects.filter(parent=diretion).values_list("id",flat=True)
            query&=Q(cate__in=allchild)
        if level:
            query&=Q(level=level)
        self.queryset=self.queryset.filter(query)
        if ordering:
            self.queryset=self.queryset.order_by(ordering)
        return self.queryset


#索引搜索视图
from drf_haystack.viewsets import HaystackViewSet
from utils.myredis import redistool
class Search(HaystackViewSet):
    index_models=[models.Course]#mysql的模型类
    serializer_class=courseser.CourseIndexSerializer# 索引的序列化器类
    pagination_class = PageCourse

    def list(self,request,*args,**kwargs):
        word=request.query_params.get("text")
        redistool.setword(word)
        return super().list(request)

class HotWord(APIView):
    def get(self,request):
        data=redistool.getword()
        if not data:
            return Response("没有数据")
        return Response(data)

class Detail(APIView):
    def get(self,request):
        id=request.query_params.get("id","")
        if not id:
            return Response("请传输id")
        info=models.Course.objects.filter(id=id).first()
        ser=courseser.CourseListSer(instance=info)
        return Response({"code":200,"msg":"ok","data":ser.data})
